"ui";
auto.waitFor();
ui.layout(
    <ScrollView>
    <vertical padding="16" h="5000dp">
        <horizontal>
            <text textSize="16sp">设备ip</text>
            <input id="PHONE_IP" text="192.168.0.12"/>
        </horizontal>
         <horizontal>
            <text textSize="16sp">检测动作</text>
            <spinner id="SPINNER_PHONE_CHECK" entries="连入|断开"/>
        </horizontal>
        <horizontal>
            <text textSize="16sp">受控制设备名称</text>
            <input id="MI_C_ID" text="公司测试插座"/>
        </horizontal>
         <horizontal>
            <text textSize="16sp">控制设备动作</text>
            <spinner id="SPINNER_MI_C_DO" entries="开|关"/>
        </horizontal>
        <horizontal>
            <text textSize="16sp">检测开始时间</text>
            <timepicker  id="TASK_STARTTIME" timePickerMode="spinner"/>
        </horizontal>
        <horizontal>
            <text textSize="16sp">检测结束时间</text>
            <timepicker  id="TASK_EDNTIME" timePickerMode="spinner"/>
        </horizontal>
        <button id="ok">确定</button>
    </vertical>
    </ScrollView>
);
//指定确定按钮点击时要执行的动作
ui.ok.click(function(){
  
  var PHONE_IP = ui.PHONE_IP.getText();
  var PHONE_CHECK = ui.SPINNER_PHONE_CHECK.getSelectedItemPosition();
  var MI_C_ID = ui.MI_C_ID.getText();
  var MI_C_DO = ui.SPINNER_MI_C_DO.getSelectedItemPosition();
  var TASK_STARTTIME=new Object();
  TASK_STARTTIME.hour = ui.TASK_STARTTIME.getCurrentHour();
  TASK_STARTTIME.min=ui.TASK_STARTTIME.getCurrentMinute();
  var TASK_EDNTIME=new Object();
  TASK_EDNTIME.hour = ui.TASK_EDNTIME.getCurrentHour();
  TASK_EDNTIME.min=ui.TASK_EDNTIME.getCurrentMinute();

 
    
  console.log(PHONE_IP,PHONE_CHECK,MI_C_ID,MI_C_DO, TASK_STARTTIME,TASK_EDNTIME);

  var config = factory.getConfig(PHONE_IP,PHONE_CHECK,MI_C_ID,MI_C_DO, TASK_STARTTIME,TASK_EDNTIME);
  
threads.start(function(){
 
var Swith = require('米家.js');
var PhoneOnline = require('Online.js');

PhoneOnline.init(config.PHONE_IP);
Swith.init(config.MI_C_ID);

  while(true){
    var myDate = new Date();
    if(dateUtil.compareHM(TASK_STARTTIME.hour,TASK_STARTTIME.min,myDate.getHours(),myDate.getMinutes())&&dateUtil.compareHM(myDate.getHours(),myDate.getMinutes(),TASK_EDNTIME.hour,TASK_EDNTIME.min)){
          var res = PhoneOnline.isChange();
          PhoneOnline.out(res);
          var cNo=false;
          if(PHONE_CHECK==0){
            cNo=true;
          }
          console.log(PHONE_CHECK,cNo,res.change)
          //受检测设备状态变化
          if(res.change){
            //状态和设置状态一直
            if(res.online==cNo){
              //执行对应动作
              if(cNo){
                PhoneOnline.out("设备已上线");
                console.log(Swith.swithType.no,MI_C_ID )
                //Swith.doExe(Swith.swithType.no,MI_C_ID);
                sleep(3*1000);
              }else{
                PhoneOnline.out("设备已下线");
                console.log(Swith.swithType.no,MI_C_ID )
                //Swith.doExe(Swith.swithType.off,MI_C_ID);
                sleep(3*1000);
              }
            }
          }
              
    }else{
         console.log("未到执行时间,等待中..."+TASK_STARTTIME.hour+":"+TASK_STARTTIME.min+"~"+TASK_EDNTIME.hour+":"+TASK_EDNTIME.min );
         sleep(1000*10);
       }
      sleep(500);
  }
});

});

var factory={
  getConfig:function(PHONE_IP,PHONE_CHECK,MI_C_ID,MI_C_DO,TASK_STARTTIME,TASK_EDNTIME){
   var config = new Object();
   config.PHONE_IP=PHONE_IP;
   config.PHONE_CHECK=PHONE_CHECK;
   config.MI_C_ID=MI_C_ID;
   config.MI_C_DO=MI_C_DO;
   config.TASK_STARTTIME=TASK_STARTTIME;
   config.TASK_EDNTIME=TASK_EDNTIME;
   return config;
  }
}

var dateUtil ={
    compareHM:function(hour,min,hour1,min1){
        if(hour==hour1){
            if(min<=min1){
                return true;
            }else {
              return false;
            }
        }else if(hour<hour1){
          return true;
        }
        return false;
    }

}












